<?php
/**
 * Class WxPayCallback 回调函数
 */
namespace V1;
use Util\LogUtil;

class WxPayCallback{

    public function post(array $req, $post, &$result){
        $aliPayData = $this->Queryorder($post);
        exit;

    }
    public function Queryorder($post){
        $post_arr = simplexml_load_string($post, 'SimpleXMLElement', LIBXML_NOCDATA);
        if ($post_arr){
            $result_code = $post_arr->result_code; //SUCCESS表示商户接收通知成功并校验成功
            $out_trade_no = $post_arr->out_trade_no; //商户订单号  5100000989201705171759311180
            $trade_no = $post_arr->transaction_id; // 微信支付订单号 1217752501201407033233368018
            $tabulation_cls  = new \Orm\Transaction\TransactionClass();
            $tabulation  = new \Orm\Transaction\TransactionList();
            $order_cls   = new \Orm\Order\OrderClass();
            $tabulation_cls ->beginTransaction();
            if($result_code=='SUCCESS'){
                try{
                    /*根据out_trade_no查询流水*/
                    $tabulation['where'] = 'out_trade_no = '.$out_trade_no.' AND status='.\Orm\Transaction\TransactionClass::STATUS_NEW;
                    $tabulation->load();
                    $tabulation_id = $tabulation['lists']['0']['id'];
                    /*如果存在执行更新操作*/
                    if ($tabulation_id) {
                        $tabulation_cls ->loadById($tabulation_id);
                        $tabulation_cls['status'] = \Orm\Transaction\TransactionClass::STATUS_SUCCEED;
                        $tabulation_cls['platform_id'] = $trade_no;
                        $tabulation_cls->update();
                        /*用reference查询订单信息更新订单表*/
                        $order_id = $tabulation_cls['reference'];
                        $order_cls ->loadById($order_id);
                        if ($order_cls) {
                            $order_cls['pay_status'] = \Orm\Order\OrderClass::PAID;
                            $order_cls['pay_datetime'] = date('Y-m-d H:i:s');
                            $order_cls->update();
                        }
                    }
                }catch (\Exception $e){
                    $tabulation_cls->rollBack();
                    throw new \Exception($e->getMessage());
                }
                $order_cls->commit();
                echo 'SUCCESS';
            }
        }
    }
}
